home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / qtools0.2-src.lha / src / libqtools / database.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-13  |  5.5 KB  |  187 lines

  1. #define    LIBQTOOLS_CORE
  2. #include <libqtools.h>
  3. #include "../liblists/liblists.h"
  4. #include "database.h"
  5.  
  6. /*
  7.  * in general the database-system allows completely transparent access to
  8.  * files and/or names out of every possible filetype
  9.  */
  10.  
  11. struct db globalDB;
  12.  
  13. /*
  14.  * ============================================================================
  15.  * GetNameEntry
  16.  *
  17.  * returns the original name of the entry or the old alias if not found
  18.  * the database will be updated automatically if the entry does not exists
  19.  * the path will verified
  20.  * ============================================================================
  21.  */
  22. char *GetNameEntryDB(struct db *database, char *aliasname)
  23. {
  24. }
  25.  
  26. /*
  27.  * ============================================================================
  28.  * GetPathEntry
  29.  *
  30.  * returns the valid path of the entry or 0
  31.  * the database will be updated automatically if the entry does not exists
  32.  * the path will verified and the original name will set to the aliasname
  33.  * ============================================================================
  34.  */
  35. char *GetPathEntryDB(struct db *database, char *aliasname)
  36. {
  37. }
  38.  
  39. char *VerifyPathDB(struct db *database, char *name)
  40. {
  41. }
  42.  
  43. /*
  44.  * ============================================================================
  45.  * LoadDatabase
  46.  * 
  47.  * loads the disk-representation of the complete database
  48.  * joins lists with the same datatype
  49.  * TODO: remove doubled entries
  50.  * ============================================================================
  51.  */
  52. bool LoadDatabaseDB(struct db *database, char *fileName)
  53. {
  54.   nNewList(db->aliasList);
  55.  
  56.   return AppendDatabaseDB(database, fileName);
  57. }
  58.  
  59. bool AppendDatabaseDB(struct db * database, char *fileName)
  60. {
  61.   FILE *dbFile;
  62.  
  63.   if ((dbFile = fopen(fileName, READ_BINARY))) {
  64.     int dbEnd;
  65.  
  66.     fseek(dbFile, 0, SEEK_END);
  67.     dbEnd = ftell(dbFile);
  68.     fseek(dbFile, 0, SEEK_BEGINNING);
  69.  
  70.     while (dbEnd > ftell(dbFile)) {
  71.       struct alist *actList, lastList;
  72.       int dbLength, i;
  73.       bool new = TRUE;
  74.  
  75.       if (!(actList = (struct alist *)tmalloc(sizeof(struct alist)))) {
  76.     eprintf("failed to allocate database\n");
  77.     return FALSE;
  78.       }
  79.       /* set filetype */
  80.       fread(&actList->fileType, 1, sizeof(filetype), dbFile);
  81.       /* read length of filetype */
  82.       fread(&dbLength, 1, sizeof(int), dbFile);
  83.  
  84.       /* look if there exists the same datatype before this */
  85.       lastList = (struct alist *)nSucc((struct nnode *)&database->aliasList));
  86.       for (i = database->aliasList.nodes; i > 0; i--) {
  87.     if (actList->fileType == lastList->fileType) {
  88.       new = FALSE;
  89.       tfree(actList);
  90.       actList = lastList;
  91.       break;
  92.     }
  93.       }
  94.  
  95.       while (dbLength > ftell(dbFile)) {
  96.     struct anode *actNode;
  97.  
  98.     if (!(actNode = (struct anode *)tmalloc(sizeof(struct anode)))) {
  99.       eprintf("failed to allocate database\n");
  100.       return FALSE;
  101.     }
  102.     /* strings cant be greater than 1k */
  103.     if (!(actNode->aliasname = (char *)tmalloc(1024))) {
  104.       eprintf("failed to allocate database\n");
  105.       return FALSE;
  106.     }
  107.     if (!(actNode->name = (char *)tmalloc(1024))) {
  108.       eprintf("failed to allocate database\n");
  109.       return FALSE;
  110.     }
  111.     if (!(actNode->filename = (char *)tmalloc(1024))) {
  112.       eprintf("failed to allocate database\n");
  113.       return FALSE;
  114.     }
  115.     /* scan the string out */
  116.     fscanf("%s\n%s\n%s\n\0", actNode->aliasname, actNode->name, actNode->filename);
  117.     /* shorten the strings to the real size */
  118.     actNode->aliasname = (char *)trealloc(actNode->aliasname, strlen(actNode->aliasname) + 1);
  119.     actNode->name = (char *)trealloc(actNode->name, strlen(actNode->name) + 1);
  120.     actNode->filename = (char *)trealloc(actNode->filename, strlen(actNode->filename) + 1);
  121.     actNode->data = *((int *)actNode->name);        /* sort by name */
  122.  
  123.     /* register to the list */
  124.     nEnqueue(&actList->listHeader, &actNode->Node);        /* register sorted */
  125.       }
  126.       fseek(dbFile, dbLength, SEEK_BEGINNING);
  127.       actList->listNode.data = actList->fileType;        /* sort by fileType */
  128.  
  129.       /* register to the list only if this is a new list */
  130.       if (new)
  131.     nEnqueue(&database->aliasList, &actList->listNode);    /* register sorted */
  132.     }
  133.  
  134.     fclose(dbFile);
  135.   }
  136.  
  137.   return TRUE;
  138. }
  139.  
  140. /*
  141.  * ============================================================================
  142.  * SaveDatabase
  143.  *
  144.  * saves the complete database to the disk-representation
  145.  * ============================================================================
  146.  */
  147. bool SaveDatabaseDB(struct db * database, char *fileName) {
  148.   FILE *dbFile;
  149.  
  150.   if ((dbFile = fopen(fileName, WRITE_BINARY))) {
  151.     struct alist *actList = (struct alist *)&database->aliasList;
  152.  
  153.     /* loop through all fileType-lists */
  154.     while ((actList = (struct alist *)nSuccNode(&actList->listNode))) {
  155.       struct anode *actNode = (struct anode *)&actList->listHeader;
  156.       int dbLength = 0, dbPos;
  157.  
  158.       /* put fileType first */
  159.       fwrite(&actList->listType, 1, sizeof(filetype), dbFile);
  160.       dbPos = ftell(dbFile);
  161.       /* then some space for the seek-able offset */
  162.       fwrite(&dbLength, 1, sizeof(int), dbFile);
  163.  
  164.       /* loop through all node of a fileType-list */
  165.       while ((actNode = (struct anode *)nSuccNode(&actNode->Node)))
  166.     fprintf("%s\n%s\n%s\n\0", actNode->aliasname, actNode->name, actNode->filename);
  167.       /* this is the offset for the next filetype */
  168.       dbLength = ftell(dbFile);
  169.       /* seek to the offset-specifier */
  170.       fseek(dbFile, dbPos, SEEK_BEGINNING);
  171.       /* save it */
  172.       fwrite(&dbLength, 1, sizeof(int), dbFile);
  173.  
  174.       /* and return to old position */
  175.       fseek(dbFile, dbLength, SEEK_BEGINNING);
  176.     }
  177.  
  178.     fclose(dbFile);
  179.   }
  180.   else {
  181.     eprintf("failed to save database %s\n", fileName);
  182.     return FALSE;
  183.   }
  184.  
  185.   return TRUE;
  186. }
  187.